<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/* *
   *
   *  Deal Model
   *  Deal (акция, сделка) -  таблица, содержащая информацию о сделке с партнером, используется для внутренних операций 
   *                          по подсчету и первичного заполнения контента на сайте, со стороны клиента.
   *  Единовременно из одного результата метода getPageDeals выходит до 20 строчек таблицы.
   *
   *  */

class Deal extends CI_Model {
  
  var $table_name = 'deals';

	function getPageDeals($page){
    $amount = 20;
    $start = ($page-1)*20;
    $data = $this->db->get($this->table_name, $amount, $start);
    return $data->result();
  }

  function addDeal($partner, $partner_id, $city, $deal_name, $description, $place_name, $company_name,
                   $deal_date_start, $coupon_date_start, $deal_date_finish, $phone, $address, $p_name, $p_email,
                   $schedule, $coupon_type, $manager, $status) {
    $p_email = strtolower($p_email);
    $datestring = "%Y %m %d";
    $timestamp = time();
    $timezone = 'UP6';
    $daylight_saving = TRUE;

    $time_gmt = gmt_to_local($timestamp, $timezone, $daylight_saving);
    $time = mdate($datestring, $time_gmt);
        
    $data = array(
    		'partner'=>$partner, 'partnerid'=>$partner_id, 'city'=>$city, 'dealname'=>$deal_name,
    		'desc'=>$description,	'placename'=> $place_name, 'companyname'=>$company_name,
    		'dealstartdate'=>$deal_date_start, 'couponstartdate'=> $coupon_date_start, 
    		'enddate'=>$deal_date_finish, 'tel'=>$phone, 'address'=>$address, 'username'=>$p_name,
    		'email'=>$p_email,	'worktime'=>$schedule, 'coupontype'=>$coupon_type, 'manager'=>$manager, 'status'=>$status,
        'adddate'=>$time
    );
    $this->db->insert($this->table_name, $data);
  }

  function editDeal($id, $partner, $partner_id, $city, $deal_name, $description, $place_name, $company_name,
                    $deal_date_start, $coupon_date_start, $deal_date_finish, $phone, $address, $p_name, $p_email,
                    $schedule, $coupon_type, $status) {
    $email = strtolower($p_email);
    $data = array(
        'partner'=>$partner, 'partnerid'=>$partner_id, 'city'=>$city, 'dealname'=>$deal_name,
        'desc'=>$description,  'placename'=> $place_name, 'companyname'=>$company_name,
        'dealstartdate'=>$deal_date_start, 'couponstartdate'=> $coupon_date_start, 
        'enddate'=>$deal_date_finish, 'tel'=>$phone, 'address'=>$address, 'username'=>$p_name,
        'email'=>$p_email, 'worktime'=>$schedule, 'coupontype'=>$coupon_type, 'status'=>$status
    );
    $this->db->where('id',$id);
    $this->db->set($data);
		$this->db->update($this->table_name);
  }

  function setStatus($id, $status) {
    $this->db->where('id', $id);
    $this->db->set(array('status' => $status));
    $this->db->update($this->table_name);
  }

  function getDealById($id){
    $this->db->where('id', $id);
    $data = $this->db->get($this->table_name);
    return $data->result();
  }

  function getDealByPartnerId($id){
    $this->db->where('partnerid', $id);
    $data = $this->db->get($this->table_name);
    return $data->result();
  }

	function belongsToMe(){

	}
	
	function pageCount(){
		$query = $this->db->get($this->table_name);
		$rowcount = $query->num_rows();
		$pagecount = 0;
		if($rowcount % 20 == 0){
			$pagecount = intval($rowcount/20);
		}else{
			$pagecount = intval($rowcount/20)+1;
		}
		return $pagecount;
	} 


}